<template>
  <div class="page-container">
    <!-- 搜索栏 -->
    <div class="search-container">
      <el-form :model="searchParams" inline size="medium">
        <el-form-item label="楼宇名称:" prop="name">
          <el-input v-model="searchParams.name" placeholder="请输入楼宇名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearchBuilding">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 添加楼宇按钮 -->
    <div class="add-building">
      <el-button v-auth-btn="'park:building:add_edit'" type="primary" size="medium" @click="onOpenBuildingEditor({})">添加楼宇</el-button>
    </div>
    <!-- 楼宇数据列表 -->
    <el-table :data="buildingList" :header-cell-style="{ background: '#f7f8f9' }" size="medium">
      <!-- 序号 -->
      <el-table-column label="序号" type="index" width="80" />
      <!-- 楼宇名称 -->
      <el-table-column label="楼宇名称" prop="name" />
      <!-- 层数 -->
      <el-table-column label="层数" prop="floors" />
      <!-- 在管面积 -->
      <el-table-column label="在管面积（m²）" prop="area" />
      <!-- 物业费 -->
      <el-table-column label="物业费（元/m²）" prop="propertyFeePrice" />
      <!-- 状态 -->
      <el-table-column label="状态">
        <template #default="{ row }">
          {{ row.status ? '租赁中' : '空置中' }}
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button v-auth-btn="'park:building:add_edit'" type="text" @click="onOpenBuildingEditor(row)">编辑</el-button>
          <el-button v-auth-btn="'park:building:remove'" type="text" @click="onDeleteBuilding(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page.sync="searchParams.page"
      :page-size="10"
      layout="->, total, prev, pager, next"
      :total="total"
      @current-change="getBuildingList"
    />
    <!-- 添加租赁合同弹窗 -->
    <BuildingEditor ref="buildingEditorRef" @update-list="getBuildingList" />
  </div>
</template>

<script>
import { getBuildingListAPI, deleteBuildingAPI } from '@/api/park/building'

import BuildingEditor from './components/BuildingEditor'

export default {
  name: 'Building',
  components: {
    BuildingEditor
  },
  data() {
    return {
      // 查询参数
      searchParams: {
        page: 1,
        pageSize: 10,
        name: '' // 楼宇名称
      },
      // 楼宇列表
      buildingList: [],
      // 楼宇列表数据总条数
      total: 0
    }
  },
  created() {
    // 获取楼宇列表
    this.getBuildingList()
  },
  methods: {
    // 获取楼宇列表
    async getBuildingList() {
      const res = await getBuildingListAPI(this.searchParams)
      this.buildingList = res.rows
      this.total = res.total
    },
    // 获取楼宇列表第一页
    onSearchBuilding() {
      this.searchParams.page = 1
      this.getBuildingList()
    },
    // 删除楼宇
    onDeleteBuilding(id) {
      this.$confirm('您确定删除该楼宇吗？', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteBuildingAPI(id)
        this.$message.success('删除成功')
        // 刷新楼宇列表
        this.getBuildingList()
      }).catch(() => {
        // console.log('已取消删除')
      })
    },
    // 打开添加租赁合同弹窗
    onOpenBuildingEditor(row) {
      this.$refs.buildingEditorRef.open(row)
    }
  }
}
</script>

<style lang="scss" scoped>
.page-container {
  .search-container {
    border-bottom: 1px solid #eee;
    .el-form {
      .el-form-item {
        margin-right: 40px;
      }
    }
  }
  .add-building {
    margin-top: 25px;
  }
  ::v-deep .el-table {
    margin: 25px 0;
    .el-icon-plus {
      border: 1px solid #eee;
    }
    .el-table__cell {
      padding: 5px 0;
    }
  }
}
</style>
